package com.leetcode.LC;

import java.util.ArrayList;
import java.util.Collections;

public class LC530 {
    ArrayList<Integer> list = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {

        int min = Integer.MAX_VALUE;
        if (root != null) {
            preTraversal(root);
        }
        Collections.sort(list);
        for (int i = 0; i < list.size() - 1; i++) {
            if(list.get(i+1)-list.get(i)<min){
                min=list.get(i+1)-list.get(i);
            }
        }
        return min;
    }

    public void preTraversal(TreeNode treeNode) {
        list.add(treeNode.val);
        if (treeNode.left != null) {
            preTraversal(treeNode.left);
        }
        if (treeNode.right != null) {
            preTraversal(treeNode.right);
        }
    }
}
